<html>
<h1>
Calculator
</h1>
<body>
(Ver 1.0 4/9/12 by David Meng and Matthew Kwong) </br>
Type an expression in the following box (e.g., 10.5+20*3/25). </br>
<form action="calculator.php" method="post">
<input type="text" name="input" />
<input type="submit" value="Calculate" />
</form>
<?php
if($_POST["input"])
{
	echo "<h2>"."Result:"."</h2>";
	$equ = $_POST["input"];

	/* Check for alphabetical characters */
	if (preg_match("#[A-Z]#i",$equ,$matches))
		echo "Invalid input expression ".$equ;

	/* Check for consecutive operators */
	else if (preg_match("#[\+]{2,10}#",$equ,$matches))
		echo "Invalid input expression ".$equ;
	else if (preg_match("#[\-]{2,10}#",$equ,$matches))
		echo "Invalid input expression ".$equ;
	else if (preg_match("#[\*][ ]*[\*]#",$equ,$matches))
		echo "Invalid input expression ".$equ;
	else if (preg_match("#[\/][ ]*[\/]#",$equ,$matches))
		echo "Invalid input expression ".$equ;

	/* Check for invalid operation combos */
	else if (preg_match("#[\+,\-,\*,\/][ ]*[\*,\/]#",$equ,$matches))
		echo "Invalid input expression ".$equ;

	/* Check for final operations */
	else if (preg_match("#[\+,\-,\*,\/]$#",$equ,$matches))
		echo "Invalid input expression ".$equ;

	/* Check for invalid initial characters */
	else if (preg_match("#^[\+,\-,\.][^(0-9)]#",$equ,$matches))
		echo "Invalid input expression ".$equ;
	else if (preg_match("#^[\*,\/]#",$equ,$matches))
		echo "invalid input expression ".$equ;

	/* Check for consecutive periods */
	else if 
(preg_match("#[\.]{1}[^\+,^\-,^\*,^\/]*[\.]{1}#",$equ,$matches))
		echo "Invalid input expression ".$equ;

	/* Check for divide by zero */
	else if (preg_match("#(/0)[0-9]*\.*[0-9]*#",$equ,$matches))
	{
		$zero = $matches[0];
		if (!preg_match("#[1-9]#",$zero))
			echo "Error: Divide by zero";
		else
		{
			eval("\$ans = $equ;");
			echo $equ." = ".$ans;
		}
	}

	else
	{
		eval("\$ans = $equ;");
		echo $equ." = ".$ans;
	}
}
?>
</body>
</html>